import os

root_dir = r"F:\86135\Documents\Sign_Language\dataset\train"
target_dir = "ants"
img_path = os.listdir(os.path.join(root_dir, target_dir))
label = target_dir.split('_')[0]
out_dir = "ants_label"
for i in img_path:
    file_name = i.split('.jpg')[0]
    with open(os.path.join(root_dir, out_dir, "{}.text".format(file_name)), 'w') as f:
        f.write(label)